package org.hxzon.workeasy.service.impl;

import java.util.Date;
import java.util.List;

import org.hxzon.project.AbstractModelService;
import org.hxzon.util.Daytime;
import org.hxzon.workeasy.model.EasyJob;
import org.hxzon.workeasy.service.EasyJobService;
import org.springframework.stereotype.Service;

@Service
public class EasyJobServiceImpl extends AbstractModelService<EasyJob> implements EasyJobService {

    public List<EasyJob> findAllJob(String username, long first, long size) {
        return dao.findList(EasyJob.class, " where p.username='" + username + "' ", first, size);
    }

    public List<EasyJob> findThisHourJob(String username, long first, long size) {
        Daytime today = new Daytime(new Date());
        return findJob(username, today.year, today.month, today.date, today.hour, EasyJob.anyTime, first, size);
    }

    public List<EasyJob> findTodayJob(String username, long first, long size) {
        Daytime today = new Daytime(new Date());
        return findJob(username, today.year, today.month, today.date, EasyJob.anyTime, EasyJob.anyTime, first, size);
    }

    public List<EasyJob> findJob(String username, int year, int month, int date, int hour, int minute, long first, long size) {
        StringBuilder hql = new StringBuilder();
        appendWhereString(username, year, month, date, hour, minute, hql);
        return dao.findList(EasyJob.class, hql.toString(), first, size);
    }

    public long countAllJob(String username) {
        return dao.countList(EasyJob.class, " where p.username='" + username + "' ");
    }

    public long countThisHourJob(String username) {
        Daytime today = new Daytime(new Date());
        return countJob(username, today.year, today.month, today.date, today.hour, EasyJob.anyTime);
    }

    public long countTodayJob(String username) {
        Daytime today = new Daytime(new Date());
        return countJob(username, today.year, today.month, today.date, EasyJob.anyTime, EasyJob.anyTime);
    }

    public long countJob(String username, int year, int month, int date, int hour, int minute) {
        StringBuilder hql = new StringBuilder();
        appendWhereString(username, year, month, date, hour, minute, hql);
        return dao.countList(EasyJob.class, hql.toString());
    }

    private void appendWhereString(String username, int year, int month, int date, int hour, int minute, StringBuilder hql) {
        int any = EasyJob.anyTime;
        hql.append(" where 1=1 ");
        if (year != any) {
            hql.append(" and (p.notifyYear=").append(year).append(" or p.notifyYear=-8)");
        }
        if (month != any) {
            hql.append(" and (p.notifyMonth=").append(month).append(" or p.notifyMonth=").append(any).append(")");
        }
        if (date != any) {
            hql.append(" and (p.notifyDate=").append(date).append(" or p.notifyDate=").append(any).append(")");
        }
        if (hour != any) {
            hql.append(" and (p.notifyHour=").append(hour).append(" or p.notifyHour=").append(any).append(")");
        }
        if (minute != any) {
            hql.append(" and (p.notifyMinute=").append(minute).append(" or p.notifyMinute=").append(any).append(")");
        }
        if (!"*".equals(username) && !"%".equals(username)) {
            hql.append(" and p.username='").append(username).append("'");
        }
    }

}
